// gymnasium.entity.ts
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  BeforeInsert,
  BeforeUpdate,
  OneToMany,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { Playground } from 'src/modules/playground/entities/playground.entity';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';

@Entity()
export class Gymnasium {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  address: string;

  @OneToMany(() => Playground, (playground) => playground.gymnasium, {
    cascade: true,
  })
  playgrounds: Playground[];

  @ManyToMany(() => Tournament)
  @JoinTable({
    name: 'gymnasium_tournament', // 连接表的名称
    joinColumn: {
      name: 'gymnasiumId', // 连接表中指向当前实体的列名
      referencedColumnName: 'id', // 当前实体的被引用列名
    },
    inverseJoinColumn: {
      name: 'tournamentId', // 连接表中指向对方实体的列名
      referencedColumnName: 'id', // 对方实体的被引用列名
    },
  })
  tournaments: Partial<Tournament>[];

  @Column()
  createBy: number;

  @CreateDateColumn()
  createTime: Date;

  @Column()
  updateBy: number;

  @UpdateDateColumn()
  updateTime: Date;

  @BeforeInsert()
  BeforeInsert() {
    this.createTime = new Date();
    this.updateTime = this.createTime;
  }

  @BeforeUpdate()
  BeforeUpdate() {
    this.updateTime = new Date();
  }
}
